//
//  NSDate+MJ.m
//  ItcastWeibo
//
//  Created by apple on 14-5-9.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "NSDate+MJ.h"

@implementation NSDate (MJ)
/**
 *  是否为今天
 */
- (BOOL)isToday
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear;
    // 1.获得当前时间的年月日
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    
    // 2.获得self的年月日
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    return
    (selfCmps.year == nowCmps.year) &&
    (selfCmps.month == nowCmps.month) &&
    (selfCmps.day == nowCmps.day);
}

/**
 *  是否为昨天
 */
- (BOOL)isYesterday
{
    // 2014-05-01
    NSDate *nowDate = [[NSDate date] dateWithYMD];
    // 2014-04-30
    NSDate *selfDate = [self dateWithYMD];
    // 获得nowDate和selfDate的差距
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
    return cmps.day == 1;
}

- (NSDate *)dateWithYMD
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy.MM.dd";
    NSString *selfStr = [fmt stringFromDate:self];
    return [fmt dateFromString:selfStr];
}

/**
 *  是否为今年
 */
- (BOOL)isThisYear
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitYear;
    
    // 1.获得当前时间的年月日
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    
    // 2.获得self的年月日
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    
    return nowCmps.year == selfCmps.year;
}

/**      2014-12-05 12:12:12   */
+ (NSString *)currentDateHourTimeString{
    NSDate *datenow = [NSDate date];                   //取得当前的系统时间
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    return [df stringFromDate:datenow];
}

/**      2014-12-05 12:12   */
+ (NSString *)currentDateToMuniteTimeString{
    NSDate *datenow = [NSDate date];                   //取得当前的系统时间
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm"];
    return [df stringFromDate:datenow];
}

/**      yyyy年MM月dd日   */
+ (NSString *)currentDateToDayString{
    NSDate *datenow = [NSDate date];                   //取得当前的系统时间
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy年MM月dd日"];
    return [df stringFromDate:datenow];
}

/**      yyyy年MM月dd日  HH时:mm分 */
+ (NSString *)currentDateToMuniteString{
    NSDate *datenow = [NSDate date];                   //取得当前的系统时间
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy年MM月dd日 HH时mm分"];
    return [df stringFromDate:datenow];
}


/**      2014/12/05 12:12:12   */
+ (NSString *)currentDateHourTimeSecondString{
    NSDate *datenow = [NSDate date];                   //取得当前的系统时间
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    return [df stringFromDate:datenow];
}

/**      2014/12/05 12:12:12M:000   */
+ (NSString *)currentDateHourTimeMillisecondString{
    NSDate *datenow = [NSDate date];                   //取得当前的系统时间(到毫秒)
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss:SSS"];
    
    return [df stringFromDate:datenow];
}

/**     12/05 12:12   */
+ (NSString *)currentDateMonthMToDayString{
    NSDate *datenow = [NSDate date];                   //取得当前的系统时间(到分)
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"MM-dd"];
    return [df stringFromDate:datenow];
}

/**      12:12:12   */

+ (NSString *)currentDateTimeString{
    NSDate *datenow = [NSDate date];                   //取得当前的系统时间
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"HH:mm:ss"];
    return [df stringFromDate:datenow];
}

/**      12:12   */

+ (NSString *)currentDateHourAndMuniteTimeString{
    NSDate *datenow = [NSDate date];                   //取得当前的系统时间
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"HH:mm"];
    return [df stringFromDate:datenow];
}

/**      2014-12-05   */
+(NSString *)currentDateString{
    NSDate *datenow = [NSDate date];                   //取得当前的系统时间
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyy/MM/dd"];
    return [df stringFromDate:datenow];
    
}
 /**      201412  */
+ (NSString *)currentDateYearAndMonthString{
    NSDate *datenow = [NSDate date];
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyyMM"];
    
    return [df stringFromDate:datenow];
}

+ (NSString *)currentDateYearAndMonth{
    NSDate *datenow = [NSDate date];
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy-MM"];
    
    return [df stringFromDate:datenow];
}

/**      20141212  */
+ (NSString *)currentDateYearToDayString
{
    NSDate *datenow = [NSDate date];
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyyMMdd"];
    return [df stringFromDate:datenow];
}

+ (NSString *)currentDateYearToDayStringWithyyyy_MM_dd
{
    NSDate *datenow = [NSDate date];
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy-MM-dd"];
    return [df stringFromDate:datenow];
}

/**      20141212010101  */
+ (NSString *)currentDateYearToSecondString
{
    NSDate *datenow = [NSDate date];
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyyMMddHHmmss"];
    return [df stringFromDate:datenow];
}

- (NSDateComponents *)deltaWithNow
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    return [calendar components:unit fromDate:self toDate:[NSDate date] options:0];
}

- (NSDateComponents *)intervalToDate:(NSDate *)date
{
    // 日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // 想比较哪些元素
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    // 比较
    return [calendar components:unit fromDate:self toDate:date options:0];
}

/**
 *  获得两个时间之间间隔的秒数
 */
+ (NSInteger)secondIntervalBetweenDateStr1:(NSString *)dateStr1 WithDateStr2:(NSString *)dateStr2{
    
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    [format setDateFormat:@"yyyy.MM.dd HH:mm:ss"];

    NSDate  *date1 = [format dateFromString:dateStr1];
    NSDate  *date2 = [format dateFromString:dateStr2];
    
    NSDateComponents *componet = [date1 intervalToDate:date2];
    DebugLog(@"compont = %@",componet);
    return  [componet valueForComponent:NSCalendarUnitSecond];
}

+ (NSDateComponents *)dateComponentsIntervalBetweenDateStr1:(NSString *)dateStr1 WithDateStr2:(NSString *)dateStr2{
    
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    [format setDateFormat:@"yyyy.MM.dd HH:mm:ss"];
    NSDate  *date1 = [format dateFromString:dateStr1];
    NSDate  *date2 = [format dateFromString:dateStr2];
    NSDateComponents *componet = [date1 intervalToDate:date2];
    return  componet;
}

//一年的第几周
+(NSString*)changeDateStringToWeekOfYearWithDateString:(NSString*)dateString
{
    //将传入时间转化成需要的格式
    NSDateFormatter *format=[[NSDateFormatter alloc]init];
    [format setDateFormat:@"yyyy-MM-ddHH:mm:ss"];
    NSDate *fromdate=[format dateFromString:dateString];
    NSCalendar*calendar = [NSCalendar currentCalendar];
    //年月日获得
    NSInteger integer =[calendar component:NSCalendarUnitWeekOfYear fromDate:fromdate];
    //一年的第几周
    NSString*weekString =[NSString stringWithFormat:@"第%ld周",(long)integer];
    
    return weekString;
}

//一年的第几季度
+(NSString*)changeDateStringToQuarterOfYearWithDateString:(NSString*)dateString
{
    //将传入时间转化成需要的格式
    NSDateFormatter *format=[[NSDateFormatter alloc]init];
    [format setDateFormat:@"yyyy-MM-ddHH:mm:ss"];
    NSDate *fromdate=[format dateFromString:dateString];
    NSCalendar*calendar = [NSCalendar currentCalendar];
    //年月日获得
    NSInteger integer =[calendar component:NSCalendarUnitQuarter fromDate:fromdate];
    //一年的第几季度
    NSString*quarterString =[NSString stringWithFormat:@"第%ld季度",(long)integer+1];
    
    return quarterString;
}

//一年的上下半年
+(NSString*)changeDateStringToParthOfYearWithDateString:(NSString*)dateString
{
    //将传入时间转化成需要的格式
    NSDateFormatter *format=[[NSDateFormatter alloc]init];
    [format setDateFormat:@"yyyy-MM-ddHH:mm:ss"];
    NSDate *fromdate=[format dateFromString:dateString];
    NSCalendar*calendar = [NSCalendar currentCalendar];
    //年月日获得
    NSInteger integer =[calendar component:NSCalendarUnitMonth fromDate:fromdate];
    
    if (integer<6)
    {
        //一年的第几周
        return @"上半年";
    }else
    {
        return @"下半年";
    }
}

+(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMinute:(int)minute{
    NSDateComponents *comps = [[NSDateComponents alloc]init];
    [comps setMinute:minute];
    NSCalendar *calender = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];// NSGregorianCalendar
    
    NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
    
    return mDate;
}


//如果日期是一星期内,便返回星期N,如果是一星期前就返回之间的日期
+(NSString *)returnDate:(NSDate *)d{
    NSDate *tmpDate =[[NSDate alloc] initWithTimeIntervalSinceReferenceDate:([[NSDate date] timeIntervalSinceReferenceDate] - 7*24*60*60)];
    
    if([tmpDate earlierDate:d] == tmpDate){
        
        NSDateComponents *componets = [[NSCalendar autoupdatingCurrentCalendar] components:NSWeekdayCalendarUnit fromDate:d];
        ;
        
        return [ NSString stringWithFormat:@"%@",[self getWeek:[componets weekday]-1]] ;
    }else{
        return [self stringFromDate:d format:@"yyyy/MM/dd"];
    }
    
}
+//nsstring 转 nsdate
(NSDate *)dateFromString:(NSString *)dateString format:(NSString *)format{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: format];
    NSDate *destDate= [dateFormatter dateFromString:dateString];
    return destDate;
}
//nsdate 转 nsstring
+(NSString *)stringFromDate:(NSDate *)date format:(NSString *)format{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    return [dateFormatter stringFromDate:date];
    
}
+(NSString *)getWeek:(NSInteger)week{
    switch (week) {
        case 1:
            return @"星期一";
            break;
        case 2:
            return @"星期二";
            break;
        case 3:
            return @"星期三";
            break;
        case 4:
            return @"星期四";
            break;
        case 5:
            return @"星期五";
            break;
        case 6:
            return @"星期六";
            break;
        default:
            return @"星期日";
            break;
    }
    
}



+(NSString *)beforeSecondDateWithTime:(NSString*)time
{
    NSString *dateStr = [NSDate currentDateHourTimeString];//当前时间
    NSTimeInterval  timeInterval = [NSDate pleaseInsertStarTime:time andInsertEndTime:dateStr];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [dateFormatter dateFromString:dateStr];
    NSDate *lastDay = [NSDate dateWithTimeInterval:-timeInterval sinceDate:date];
    return [dateFormatter stringFromDate:lastDay];
}

/**  前一个天     2014-12-05   */
+(NSString *)lastDayDateString{
    
    NSDate *date = [NSDate date];//当前时间
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:date];//前一天
    return [dateFormatter stringFromDate:lastDay];
    
}

/**  前一周        */
+(NSString *)lastWeekDateString{
    
    NSDate *date = [NSDate date];//当前时间
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
    
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    
    NSInteger interval = [zone secondsFromGMTForDate: date];
    
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    
    NSDate *lastWeek = [NSDate dateWithTimeInterval:-24*60*60*7 sinceDate:localeDate];//前一天
    
    return [dateFormatter stringFromDate:lastWeek];
    
}

/**  前一个月        */
+(NSString *)lastMouthDateString{
    
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];
    NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
    
    //获取前一个月的时间
    NSDate *monthagoData = [self getPriousorLaterDateFromDate:currentDate withMonth:-1];
    return  [dateFormatter stringFromDate:monthagoData];
}

+(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(int)month{
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    
    [comps setMonth:month];
    
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];// NSGregorianCalendar
    NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
    return mDate;
}

//传入秒得到时分秒算法

+(NSString *)getMMSSFromSS:(NSString *)totalTime{
    
    NSInteger seconds = [totalTime integerValue];
    
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02d",seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02d",(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02d",seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@",str_minute,str_second];
    
    return format_time;
}


//时间转转时间
+(NSString *)stringTimeChangeStringTime:(NSString *)stringTime
{
    //将传入时间转化成需要的格式
    NSDateFormatter *format=[[NSDateFormatter alloc]init];
    [format setDateFormat:@"yyyy年MM月dd日"];
    NSDate *fromdate=[format dateFromString:stringTime];
    
    NSDateFormatter *format1 = [[NSDateFormatter alloc] init];
    
    format1.dateFormat = @"yyyyMMdd";
    
    NSString *stringDate = [format1 stringFromDate:fromdate];
    
    return stringDate;
}

//时间转转时间
+(NSString *)stringDateChangeStringDate:(NSString *)stringDate;
{
    //将传入时间转化成需要的格式
    NSDateFormatter *format=[[NSDateFormatter alloc]init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *fromdate=[format dateFromString:stringDate];
    
    NSDateFormatter *format1 = [[NSDateFormatter alloc] init];
    
    format1.dateFormat =@"yyyy年MM月dd日";
    
    NSString *string = [format1 stringFromDate:fromdate];
    
    return string;
}

+(NSString *)stringDateChangeStringDate_yyyy_MM_dd_HH_mm:(NSString *)stringDate
{
    //将传入时间转化成需要的格式
    NSDateFormatter *format=[[NSDateFormatter alloc]init];
    [format setDateFormat:@"yyyy年MM月dd日 HH时mm分"];
    
    NSDate *fromdate=[format dateFromString:stringDate];
    
    NSDateFormatter *format1 = [[NSDateFormatter alloc] init];
    
    format1.dateFormat =@"yyyy/MM/dd";
    
    NSString *string = [format1 stringFromDate:fromdate];
    
    return string;
}

+(NSString *)stringDateChangeStringDate_yyyy_MM_dd:(NSString *)stringDate
{
    //将传入时间转化成需要的格式
    NSDateFormatter *format=[[NSDateFormatter alloc]init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *fromdate=[format dateFromString:stringDate];
    
    NSDateFormatter *format1 = [[NSDateFormatter alloc] init];
    
    format1.dateFormat =@"yyyy-MM-dd";
    
    NSString *string = [format1 stringFromDate:fromdate];
    
    return string;
}




/**
 方法名:   两个时间之间周数间隔
 @param   dateStr1
 @param   dateStr2
 @return  Bool 是否
 */

+ (BOOL)weekIntervalBetweenDateStr1:(NSString *)dateStr1 WithDateStr2:(NSString *)dateStr2
{
    
    NSRange yearRange1;
    NSRange quarterRange1;
    if (dateStr1.length==8) {
        
       yearRange1     = NSMakeRange(0, 4);
       quarterRange1  = NSMakeRange(6, 1);
        
    }else
    {
        yearRange1     = NSMakeRange(0, 4);
        quarterRange1  = NSMakeRange(6, 2);
    }
    
    NSRange yearRange2;
    NSRange quarterRange2;
    
    if (dateStr2.length==8) {
        
        yearRange2     = NSMakeRange(0, 4);
        quarterRange2  = NSMakeRange(6, 1);
        
    }else
    {
        yearRange2     = NSMakeRange(0, 4);
        quarterRange2  = NSMakeRange(6, 2);
    }
    
    NSString*yearStr1=[dateStr1 substringWithRange:yearRange1];
    NSString*weekStr1=[dateStr1 substringWithRange:quarterRange1];
    
    NSString*yearStr2=[dateStr2 substringWithRange:yearRange2];
    NSString*weekStr2=[dateStr2 substringWithRange:quarterRange2];
    
    if ([yearStr1 intValue]>[yearStr2 intValue])
    {
        
        return NO;
    }else if ([yearStr1 intValue]==[yearStr2 intValue])
    {
        
        if ([weekStr1 intValue]>[weekStr2 intValue])
        {
           return NO;
        }else if ([weekStr1 intValue]+20<[weekStr2 intValue])
        {
            return NO;
        }
    
    }else  if ([yearStr1 intValue]+1<[yearStr2 intValue])
    {
        return NO;
        
    }else if ([weekStr1 intValue]>34)
        
    {
        if ([yearStr1 intValue]+1==[yearStr2 intValue])
        {
            if ([weekStr2 intValue]<=[weekStr1 intValue]-34)
            {
                
            }else
            {
                
                return NO;
                
            }
            
        }else
        {
            return NO;
            
        }
       
    }else if ([weekStr1 intValue] <=34)
    {
        if ([yearStr1 intValue]==[yearStr2 intValue])
        {
            
        }else
        {
            return NO;
            
        }
        
    }
    
    return YES;
}

/**
 方法名:   两个时间之间季度间隔
 @param   dateStr1
 @param   dateStr2
 @return  Bool 是否
 */

+ (BOOL)quarterIntervalBetweenDateStr1:(NSString *)dateStr1 WithDateStr2:(NSString *)dateStr2{
    
    NSRange yearRange     = NSMakeRange(0, 4);
    NSRange quarterRange  = NSMakeRange(4, 1);
    
    NSString*yearStr1=[dateStr1 substringWithRange:yearRange];
    NSString*quarterStr1=[dateStr1 substringWithRange:quarterRange];

    NSString*yearStr2=[dateStr2 substringWithRange:yearRange];
    NSString*quarterStr2=[dateStr2 substringWithRange:quarterRange];
    
    if ([yearStr1 intValue]>[yearStr2 intValue])
    {
        
        return NO;
    }
    
    if ([yearStr1 intValue]+5<[yearStr2 intValue])
    {
         return NO;
        
    
    }else if ([yearStr1 intValue]+5==[yearStr2 intValue])
        
    {
        if ([quarterStr1 intValue]<=[quarterStr2 intValue])
        {
            
         return NO;
        }

    }
    
    return YES;
}

/**
 方法名:  日期转换为星期几
 @param  format   开始时间
 @return NSString 星期几
 */

+(NSString*)weekDayStr:(NSString*)format{
    
    NSString *weekDayStr = nil;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    if(format.length>=10) {
        NSString *nowString = [format substringToIndex:10];
        NSArray *array = [nowString componentsSeparatedByString:@"-"];
        if(array.count==0) {
            array = [nowString componentsSeparatedByString:@"/"];
        }
        
        if(array.count>=3) {
            NSInteger year = [[array objectAtIndex:0] integerValue];
            NSInteger month = [[array objectAtIndex:1] integerValue];
            NSInteger day = [[array objectAtIndex:2] integerValue];
            [comps setYear:year];
            [comps setMonth:month];
            [comps setDay:day];
        }
    }
    //日历
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    //获取传入date
    NSDate *_date = [gregorian dateFromComponents:comps];
    NSDateComponents *weekdayComponents = [gregorian components:NSCalendarUnitWeekday fromDate:_date];
    NSInteger week = [weekdayComponents weekday];
    switch(week) {
        case 1:
            weekDayStr =@"星期日";
            break;
        case 2:
            weekDayStr =@"星期一";
            break;
        case 3:
            weekDayStr =@"星期二";
            break;
        case 4:
            weekDayStr =@"星期三";
            break;
        case 5:
            weekDayStr =@"星期四";
            break;
        case 6:
            weekDayStr =@"星期五";
            break;
        case 7:
            weekDayStr =@"星期六";
            break;
        default:
            weekDayStr =@"";
            break;
    }
    return weekDayStr;
}
/**
 方法名:  比较两个时间差值秒
 @param starTime 开始时间秒
 @param endTime  结束时间秒
 @return  NSTimeInterval 时间差值（单位秒）
 */

+ (NSTimeInterval)pleaseInsertStarTime:(NSString *)starTime andInsertEndTime:(NSString *)endTime{
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//根据自己的需求定义格式
    NSDate* startDate = [formater dateFromString:starTime];
    NSDate* endDate = [formater dateFromString:endTime];
    NSTimeInterval time = [endDate timeIntervalSinceDate:startDate];
    return time;
}


/**
 方法名:  比较两个时间差值毫秒
 @param starTime 开始时间毫秒
 @param endTime  结束时间毫秒
 @return  NSTimeInterval 时间差值（单位秒）
 */

+ (NSTimeInterval)pleaseInsertWithMilliSecondStarTime:(NSString *)starTime andInsertEndTime:(NSString *)endTime{
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss:SSS"];//根据自己的需求定义格式
    NSDate* startDate = [formater dateFromString:starTime];
    NSDate* endDate = [formater dateFromString:endTime];
    NSTimeInterval time = [endDate timeIntervalSinceDate:startDate];
    return time;
}


/*
 *  方法名    转换消息时间
 *  @param   NSString     booktime      消息时间
 *  @param   NSString                   转换后的消息时间 星期五或者上午 11:55 或者 昨天
 */

+(NSString*)changeBooktime:(NSString *)booktime
{
    NSDate*date = [NSDate dateFromString:booktime format:@"yyyy-MM-dd HH:mm:ss"];
    NSString*dateStr;
    if (date!=nil)
    {
     if ([date isToday])
     {
        dateStr = [NSDate stringFromDate:date format:@"aa h:mm"];
     }else
     {
        dateStr = [date isYesterday]==YES?@"昨天":[NSDate returnDate:date];
      }
    }
    return dateStr;
}

/*
 *  方法名    转换消息时间
 *  @param   NSString     dateTime      消息时间
 *  @param   NSString                   转换后的消息时间 星期五或者上午 11:55 或者 昨天:10:202
 */

+(NSString*)changeWithDateTime:(NSString *)dateTime
{
    NSDate*date = [NSDate dateFromString:dateTime format:@"yyyy-MM-dd HH:mm:ss"];
    NSString*dateStr;

    if (date!=nil)
    {
        if ([date isToday])
        {
            dateStr =[NSDate stringFromDate:date format:@"aa h:mm"];
        }else
        {
            if ([date isYesterday])
            {
                dateStr =[NSString stringWithFormat:@"昨天  %@",[NSDate stringFromDate:date format:@"aa h:mm"]];
            }else
            {
                dateStr =[NSString stringWithFormat:@"%@ %@",[NSDate returnDate:date],[NSDate stringFromDate:date format:@"aa h:mm"]];
            }
        }
    }
    return dateStr;
}

@end
